package com.derbysoft.ota.application.translator.rs;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.application.util.Calendars;
import com.derbysoft.ota.interfaces.protocol._2009a.BasicPropertyInfoType;
import com.derbysoft.remote.dswitch.hotel.dto.HotelPolicyDTO;

public class HotelPolicyTranslator implements Translator<HotelPolicyDTO, BasicPropertyInfoType.Policy> {
	
	private static Pattern pattern = Pattern.compile("^\\d{2}:\\d{2}$");
	
    @Override
    public BasicPropertyInfoType.Policy translate(HotelPolicyDTO source) {
    	if (source == null) {
    		return null;
    	}
    	if (!formatMatched(source.getCheckInTime()) || !formatMatched(source.getCheckOutTime())) {
    		return null;
    	}
    	
        BasicPropertyInfoType.Policy policy = new BasicPropertyInfoType.Policy();
        policy.setCheckInTime(Calendars.of(source.getCheckInTime(), format()));
        policy.setCheckOutTime(Calendars.of(source.getCheckOutTime(), format()));
		return policy;
    }

	private DateFormat format() {
		return new SimpleDateFormat("HH:mm");
	}

	private boolean formatMatched(String time) {
		if (StringUtils.isBlank(time)) {
			return false;
		}
		return pattern.matcher(time).matches();
	}
}
